// app.js
import utils from './utils/utils.js'
App({
  globalData: {
    token:'',
    navBarHeight:'',
    statusBarHeight:'',
    windowHeight:'',
  },
data: {
  shareId: ''
},
  onLaunch(options) {
    console.log(options, "分享")
    if(options.query.scene){
      let val = decodeURIComponent(options.query.scene);
      let id = this.getQueryValue(val, 'id');
      console.log(`通过别人的二维码扫进来的,他的id ====>${id}`);
      this.data.shareId = id;
    }else if(options.query.id){
      this.data.shareId = options.query.id;
    }
    this.handleLogin();
    const vm = this;
    //获取系统信息
    let systemInfo = wx.getSystemInfoSync(),
        isiOS = systemInfo.system.indexOf('iOS') > -1,
        // statusBarHeight = systemInfo.statusBarHeight,
        navHeight='';
    if (!isiOS) {                
      navHeight = 48;            
    } else {                
      navHeight = 44;            
    }  
    /**
     * 获取胶囊高度
     * */ 
    const res = wx.getMenuButtonBoundingClientRect();
    console.log(res,statusBarHeight)
    let statusBarHeight =res.top;
    vm.globalData.navBarHeight = res.height;
    // vm.globalData.navBarHeight = navHeight;
    vm.globalData.statusBarHeight = statusBarHeight;
    this.globalData.windowHeight= wx.getSystemInfoSync().windowHeight
    this.checkNewVersion()
  },

  /**
  * 检查版本更新
  */
  checkNewVersion: function () {
    
    //获取小程序更新机制兼容
    if (wx.canIUse("getUpdateManager")) {
      //创建 UpdateManager 实例
      const updateManager = wx.getUpdateManager();
      //检测版本更新
      updateManager.onCheckForUpdate(function (res) {
        //请求信版本信息的回调
        if (res.hasUpdate) {
          updateManager.onUpdateReady(function () {
            wx.showModal({
              title: "更新提示",
              content: "新版本已经准备好，是否重启应用？",
              success: function (res) {
                if (res.confirm) {
                  // 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
                  updateManager.applyUpdate();
                }
              }
            });
          })
          // 新的版本下载失败
          updateManager.onUpdateFailed(function () {
            wx.showModal({
              title: "已经有新版本了哟~",
              content: "请您删除当前微信小程序，重新搜索打开哦~"
            });
          });
        }
      })
    } else {
      wx.showModal({
        title: "溫馨提示",
        content: "当前微信版本过低，无法使用该功能，请升级到最新版本后重试。"
      });
    }
   
  },

  /**
   * 解析url参数
   * */ 
  getQueryValue(query, queryName) {
    const reg = new RegExp("(^|&)" + queryName + "=([^&]*)(&|$)", "i");
    const r = query.match(reg);
    if (r !== null){
      return r[2];
    } else {
      return null;
    }
  },

  /**
   * 登录
   */
  handleLogin:function(){
    const vm = this;
    wx.login({
      success (res) {
        wx.setStorageSync('code', res.code);
        if (res.code) {
          //发起网络请求
          utils.get("user/getToken",{
            code:res.code,
            shareId: vm.data.shareId
          }).then((res)=>{
            // vm.globalData.token = res.session;
            wx.setStorageSync('token', res.session);
            wx.setStorageSync('userInfo', res);
          })
        } else {
          console.log('登录失败！' + res.errMsg)
        }
      }
    })
  }
})
